//
//  RJCheckDataFitCell.m
//  AirQuality
//
//  Created by Po on 15/5/2.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCheckDataFitCell.h"

@interface RJCheckDataFitCell()

@property (assign, nonatomic) CGFloat cellWidth;
@property (assign, nonatomic) NSUInteger buttonNum;
@property (retain, nonatomic) UIView * line;
@end

@implementation RJCheckDataFitCell

- (instancetype)initWithFrame:(CGRect)frame buttonNum:(NSUInteger)num
{
    self = [super initWithFrame:frame];
    if (self) {
        _cellWidth = frame.size.width;
        _lineColor = [UIColor whiteColor];
        [self createAllButtonWithNum:num];
        self.contentView.backgroundColor = [UIColor clearColor];
    }
    return self;
}
- (void)setButtonNum:(NSUInteger)num
{
    _cellWidth = ScreenWidth;
    [self emptyCell];
    [self createAllButtonWithNum:num];
}

- (void)setAllEfficacy:(BOOL)isEfficacy
{
    for (RJCheckDataCellButton * button in [self.contentView subviews]) {
        [button setUserInteractionEnabled:isEfficacy];
    }
}

- (void)setAllFont:(UIFont *)font
{
    for (RJCheckDataCellButton * button in [self.contentView subviews]) {
        if ([button isKindOfClass:[RJCheckDataCellButton class]]) {
            [button.titleLabel setFont:font];
        }
    }
}

- (void)createAllButtonWithNum:(NSUInteger)num
{
    _buttonNum = num;
    if (num < 1) {
        return;
    }
    
    NSUInteger maxNum = num;
    
    CGFloat width = _cellWidth / maxNum;
    CGFloat height = self.frame.size.height - 4;
    _allButtons = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < num; i ++) {
        RJCheckDataCellButton * button = [[RJCheckDataCellButton alloc] initWithFrame:CGRectMake(width * i, 2, width, height)];
        [button setTitleColor:GetRGBColor(80, 80, 80, 1) forState:UIControlStateNormal];
        [_allButtons addObject:button];
        [self.contentView addSubview:button];

        if (i != 0) {
            [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
            [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
            button.typeNumber = i - 1;
        } else {
            [button.titleLabel setTextAlignment:NSTextAlignmentLeft];
        }
    }
}

-(void)drawRect:(CGRect)rect
{
    if (_line) {
        [_line setFrame:CGRectMake(20, self.frame.size.height - 1, self.frame.size.width - 40, 1)];
        [_line setBackgroundColor:_lineColor];
    }
}


- (void)showLine
{
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
        [_line setBackgroundColor:_lineColor];
        [self.contentView addSubview:_line];
    }
}

- (void)setLineColor:(UIColor *)color
{
    _lineColor = color;
    if (_line) {
        [_line setBackgroundColor:_lineColor];
    }
    
}

/**清空Cell中所有控件*/
- (void)emptyCell
{
    for (RJCheckDataCellButton * button in _allButtons) {
        [button removeFromSuperview];
    }
    [_allButtons removeAllObjects];
}

#pragma mark - event
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)pressButton:(RJCheckDataCellButton *)sender
{
    if (_delegate) {
        [_delegate RJCheckDataFitCellDidSelect:sender cell:self];
    }
}


@end
